<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class schedule_model extends CI_Model {
	//모델 생성자 호출
	function __construct(){
		parent::__construct();
		$this->load->helper('file');
	}
	
	/**
	 * 행사 내용 조회
	 *
	 * @access	public
	 * @return	array
	 */
	function select($id){
		$this->db->select("*");
	  	$this->db->where('id', $id);
	  	$query = $this->db->get('ch_schedule');
	  	//echo $this->db->last_query();
	  	$row = $query->row_array();
	  	if($row){
	  		$data['dbjob'] = 'u';
	  		$data['id'] = $row['id']; 
	  		$data['title'] = $row['title'];
	  		$data['schedule_date'] = substr($row['schedule_date'],0,10);
	  		$data['schedule_time'] = $row['schedule_time'];
	  		$data['content'] = $row['content'];
	  	}else{
	  		$data['dbjob'] = 'i';
	  		$data['id'] = null; 
	  		$data['title'] = null;
	  		$data['schedule_date'] = null;
	  		$data['schedule_time'] = null;
	  		$data['content'] = null;
	  	}
	  	return $data;
	}
	
	/**
	 * 행사 검색조건
	 *
	 * @access	public
	 * @return	query where절 구문
	 */
	function list_where($param){
		$sch_duty = $param['sch_duty'];
		if($sch_duty){
			return $this->db->where(array('duty'=>$sch_duty));
		}else{
			return null;
		}
	}
	
	/**
	 * 행사 db 처리
	 *
	 * @access	public
	 * @return	void
	 */
	function dbjob($data){
		$dbjob = $this->input->post('dbjob');
		$id = $this->input->post('id');
		switch($dbjob){
			case('i'):
				$this->db->insert('ch_schedule',$data);
			break;
			case('u'):
				$this->db->where('id',$id);
				$this->db->update('ch_schedule',$data);
			break;
			case('d'):
				$this->db->delete('ch_schedule',$data);
			break;
		}
	}
	
	/**
	 * 행사 총갯수
	 *
	 * @access	public
	 * @return	integer
	 */
	function list_total($param){
		$this->list_where($param);
		$count = $this->db->count_all_results('ch_schedule');
		return $count;
	}
	
	/**
	 * 행사 리스트
	 *
	 * @access	public
	 * @return	result
	 */
	function schedule_list($param){
		$this->db->where(array('SUBSTRING(schedule_date,1,7)'=>$param['yymm']));
		$this->db->order_by('schedule_date,id');
		$query = $this->db->get('ch_schedule');
		//echo $this->db->last_query();
		return $query->result();
	}
}
?>